﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UtilityLibrary;

namespace MapLibrary.Players.Abilities
{
    class Gliding : Ability
    {
        private const float maxPower = 100.0f;
        private const float regenRate = 0.015f;
        private const float decayRate = 0.5f;
        private const float minFallSpeed = 0.1f;
        private const float damping = -0.15f;
        
        private float power;

        private Player player;

        public bool Active { get; set; }

        public Gliding(Player p)
        {
            player = p;
            power = maxPower;
        }

        public override bool ActivateConditionsSatisfied(Player p)
        {
            return (power > decayRate) && (player.Body.LinearVelocity.Y > 0.0f);
        }

        public override void Activate(Player p)
        {
            Active = true;
            Vector2 velocity = p.Body.LinearVelocity;
            bool right = velocity.X > 0;
            velocity.Y += damping;
            if (velocity.Y < minFallSpeed)
            {
                velocity.Y = minFallSpeed;
            }
            p.Body.LinearVelocity = velocity;

            if (velocity.X > 0)
            {
                p.Type.CurrentState = "Gliding Right";
            }
            else
            {
                p.Type.CurrentState = "Gliding Left";
            }
        }

        public override void Update()
        {
            if (Active && player.Body.LinearVelocity.Y > 0.0f)
            {
                power -= decayRate;
                if (power <= 0.0f)
                {
                    Active = false;
                    power = 0.0f;
                }
            }
            else
            {
                Active = false;
                power += regenRate;
                if (power > maxPower)
                {
                    power = maxPower;
                }
            }
        }


        public void DrawAtPlayer(SpriteBatch batch, WorldViewport viewport, Player p)
        {
            Vector2 position = viewport.WorldToViewportCoords(p.Position);
            position = position - p.GetCenter() - new Vector2(0, 90);
            string s = string.Format("Gliding: {0}/{1}", power.ToString(), maxPower.ToString());
            PrimitiveBrush.RenderText(batch, s, position);
        }

    }

    
}
